#ifndef FramelessWidget_H
#define FramelessWidget_H
#include <QWidget>
#include <QDialog>
#include <QVBoxLayout>
#include <QPainterPath>
#include <QPainter>
#include <QtMath>
#include <QDesktopWidget>
#include <QApplication>
#include <QDebug>
#include <QCloseEvent>
#include <QMainWindow>

#include "titleRibbon/titlebar.h"
#include "frameless/frameless_helper.h"
/*********************************************************/
// 最大字符串长度
static const int CharMaxLen = 100;
// 全局变量皮肤主题文件名
extern char g_skinQssFile[CharMaxLen];
// 设定皮肤
void setSkin(QWidget *widget = nullptr);
// 阴影宽度
static const int WidgetShadoWidth = 3;
/*********************************************************/
// 无边框界面基类
class FramelessWidget: public QWidget
{
    Q_OBJECT
public:
    FramelessWidget(QWidget* parent = nullptr);
    ~FramelessWidget();

public:
    // 为子类添加widget留出接口
    void addWidget(QWidget *widget         = nullptr,
                   int stretch             = 0,
                   Qt::Alignment alignment = Qt::Alignment()) {
        if(widget)
            layout->addWidget(widget, stretch, alignment);
    }
    // 为子类添加layout留出接口
    void addLayout(QLayout *lay = nullptr,
                   int stretch  = 0) {
        if(layout)
            layout->addLayout(lay, stretch);
    }

    // 控制子类缩放
    void setWidgetResizable(bool b = true) {
        fh->setWidgetResizable(b);
    }
    // 窗口居中
    void setCentered() {
        QDesktopWidget *desktop = QApplication::desktop();
        move((desktop->width() - this->width()) / 2,
             (desktop->height() - this->height()) / 2);
    }
    //
    void addStretch() {
        layout->addStretch();
    }

protected:
    void drawShadow(QPainter &painter);
    void paintEvent(QPaintEvent *event) {
        Q_UNUSED(event)
        QPainter painter(this);
        this->drawShadow(painter);
        painter.setPen(Qt::NoPen);
        painter.setBrush(Qt::white);
        painter.drawRect(QRect(WidgetShadoWidth,
                               WidgetShadoWidth,
                               this->width()-2 * WidgetShadoWidth,
                               this->height()-2 * WidgetShadoWidth));
    }

private:
    QVBoxLayout       *layout = nullptr;
    FramelessHelper *fh     = nullptr;
};
/*********************************************************/
// 无边框对话框
class FramelessDialog : public FramelessWidget
{
    Q_OBJECT
public:
    FramelessDialog(QWidget* parent = nullptr);
    ~FramelessDialog();
public:
    void setTitle(const QString& title ) {
        titleBar->setElement(TitleBarElementType::TITLE, title);
    }


private:
    MainWindowTitleBar *titleBar = nullptr;
};



/*********************************************************/
// 无边框消息框
class MessageDialog : public QDialog
{
    Q_OBJECT
public:
    // 对话框消息类型
    enum MSGType {
        INFORMATION,  //询问
        WARNING,      //警告
        CRITICAL      //致命错误
    };
    // 对话框按钮
    enum DialogButton {
        ACCEPT,
        REJECT
    };

public:
    MessageDialog(const QString& title, MSGType _type, const QString &_message, QWidget* parent = nullptr);
    ~MessageDialog();
    void setButtonVisualable(DialogButton bt, bool b);

protected:
    void initDialog(const QString& title);
    void setType(MSGType _type, const QString &_message);
    void initButtons();
protected:
    // 窗口居中
    void setCentered() {
        QDesktopWidget *desktop = QApplication::desktop();
        move((desktop->width() - this->width()) / 2,
             (desktop->height() - this->height()) / 2);
    }
    void drawShadow(QPainter &painter);
    void paintEvent(QPaintEvent *event) {
        Q_UNUSED(event)
        QPainter painter(this);
        this->drawShadow(painter);
        painter.setPen(Qt::NoPen);
        painter.setBrush(Qt::white);
        painter.drawRect(QRect(WidgetShadoWidth,
                               WidgetShadoWidth,
                               this->width()-2 * WidgetShadoWidth,
                               this->height()-2 * WidgetShadoWidth));
    }

private:
    QVBoxLayout *layout         = nullptr;
    //NcFramelessHelper *fh       = nullptr;
    FramelessHelper *fh     = nullptr;

    MainWindowTitleBar *titleBar= nullptr;

    QPushButton *acceptButton   = nullptr;
    QPushButton *rejectButton   = nullptr;
    MSGType type                = INFORMATION;
};


/*********************************************************/
// 无边框QMainWindow
class FramelessMainWindow : public QMainWindow
{
    Q_OBJECT
public:
    FramelessMainWindow();
    ~FramelessMainWindow();
public:
    // 为子类添加widget留出接口
    void addWidget(QWidget *widget         = nullptr,
                   int stretch             = 0,
                   Qt::Alignment alignment = Qt::Alignment()) {
        if(widget)
            layout->addWidget(widget, stretch, alignment);
    }
    // 为子类添加layout留出接口
    void addLayout(QLayout *lay = nullptr,
                   int stretch  = 0) {
        if(lay)
            layout->addLayout(lay, stretch);
    }
    // 控制子类缩放
    void setWidgetResizable(bool b = true) {
        fh->setWidgetResizable(b);
    }
    //窗口居中
    void setCentered() {
        QRect r = geometry();
        r.moveCenter(QApplication::desktop()->availableGeometry().center());
        setGeometry(r);
    }
protected:
    void drawShadow(QPainter &painter);
    void paintEvent(QPaintEvent *event) {
        Q_UNUSED(event)
        QPainter painter(this);
        this->drawShadow(painter);
        painter.setPen(Qt::NoPen);
        painter.setBrush(Qt::white);
        painter.drawRect(QRect(WidgetShadoWidth,
                               WidgetShadoWidth,
                               this->width()-2 * WidgetShadoWidth,
                               this->height()-2 * WidgetShadoWidth));
    }
private:
    FramelessHelper *fh     = nullptr;

    QVBoxLayout *layout = nullptr;

};




#endif // FramelessWidget_H
